package com.treasure.mall.biz.service.core.user.impl;

import com.treasure.mall.biz.entity.user.MerchantPO;
import com.treasure.mall.biz.entity.user.MerchantShopFollowPO;
import com.treasure.mall.biz.repository.user.MerchantShopFollowRepository;
import com.treasure.mall.biz.service.core.user.MerchantService;
import com.treasure.mall.biz.service.core.user.MerchantShopFollowService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.service.core.user.impl
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/14 15:52
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.biz.service.core.user.impl Inc. All rights reserved.
 */
@Service
public class MerchantShopFollowServiceImpl implements MerchantShopFollowService {

    @Resource
    private MerchantShopFollowRepository repository;

    /**
     * 是否关注
     *
     * @param userId
     * @param merchantId
     * @return
     */
    @Override
    public boolean followed(Long userId, Long merchantId) {
        return repository.countByMerchantIdAndUserId(merchantId, userId) > 0;
    }

    /**
     * 关注数量
     *
     * @param merchantId
     * @return
     */
    @Override
    public long follows(Long merchantId) {
        return repository.countByMerchantId(merchantId);
    }


    @Override
    public void follow(Long userId, Long merchantId) {
        if (followed(userId, merchantId)) {
            return;
        }
        MerchantShopFollowPO po = new MerchantShopFollowPO();
        po.setMerchantId(merchantId);
        po.setUserId(userId);
        repository.save(po);
    }

    @Override
    public void unfollow(Long userId, Long merchantId) {
        MerchantShopFollowPO po = repository.findByMerchantIdAndUserId(merchantId, userId);
        if (po != null) {
            repository.deleteById(po.getId());
        }
    }
}
